<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{admin/layout}">

<head>
	<title>Record Manage</title>
	<style>

	</style>
</head>

<body layout:fragment="content">
	<div class="card shadow">
		<div class="card-header border-bottom d-flex justify-content-between align-items-center">
			<h5 class="card-title mb-0">Record List</h5>
			<div class="d-grid gap-2 d-md-block">
				<!--<button class="btn btn-sm btn-primary" type="button" onclick="modifyRecordModal('')">New</button>-->
			</div>
		</div>
		<div class="card-body">
			<table id="recordTable"></table>
		</div>
		<!--<div class="card-footer pt-0">
			Footer
		</div>-->
	</div>
	<!---->
	<div class="modal fade" id="recordModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Record Setting</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<form id="recordForm">
								<input type="hidden" class="form-control" value="" id="recordId" name="recordId">
								<div class="mb-3">
									<label class="form-label">Book-Date</label>
									<input type="text" class="form-control" value="" id="recordDate" name="recordDate">
								</div>
								<div class="mb-3">
									<label class="form-label">Unit-Price</label>
									<input type="text" class="form-control" value="" id="recordPrice" name="recordPrice">
								</div>
								<!-- Save button -->
								<div class="d-flex justify-content-end mt-4">
									<button type="button" class="btn btn-default"
										onclick="closeRecordModal()">Close</button>
									<button type="submit" class="btn btn-primary">Submit</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal -->
	</div>
	<script layout:fragment="script">
		var recordFormValidate = null
		$(document).ready(function () {
			recordFormValidate = 
			$("#recordForm").validate({
				rules: {
					recordId: {
						required: true
					},
					recordSportId: {
						required: true
					},
					recordSlotId: {
						required: true
					},
					recordUserId: {
						required: true
					},
					recordPrice: {
						required: true
					},
					recordTime: {
						required: true
					},
					recordPay: {
						required: true
					},
					recordPayTime: {
						required: true
					},
					recordConfirm: {
						required: true
					},
					recordConfirmTime: {
						required: true
					},
				},
				submitHandler: function (form) {
					var formData = $(form).serialize();
					saveRecord(formData);
					return false
				}
			})
			loadRecordTable()
			$('#recordDate').datetimepicker({
			    language: 'en',
			    autoclose: 1,
			    format: 'yyyy-mm-dd',
			    minView: "month",
			    todayBtn: 1,
			    todayHighlight: 1,
			   // defaultDate: new Date(),
			  }).on('changeDate', function(e) {
				  
			  });
		});
		function loadRecordTable() {
			$('#recordTable').bootstrapTable('destroy');
			$('#recordTable').bootstrapTable({
				url: "[[@{'/admin/records'}]]",
				uniqueId: 'recordId',
				dataType: "json",
				pagination: true,
				pageSize: 10,
				queryParams: function (params) {
					var temp = {
						size: params.limit,
						page: (params.offset / params.limit),
						sort: params.sort,
						sortOrder: params.order
					};
					return temp;
				},
				sidePagination: "server",
				pageList: [30, 50],
				columns: [{
					title: 'Sort',
					formatter: function (value, row, index) {
						var pageSize = $('#recordTable').bootstrapTable('getOptions').pageSize;
						var pageNumber = $('#recordTable').bootstrapTable('getOptions').pageNumber;
						return pageSize * (pageNumber - 1) + index + 1;
					}
				}, {
					field: 'recordSportId',
					title: 'The-Sport',
					formatter: function (value, row, index) {
						return row.sport.sportTitle
					}
				}, {
					field: 'recordUserId',
					title: 'Booker',
					formatter: function (value, row, index) {
						return row.user.userName
					}
				}, {
					field: 'recordSlotId',
					title: 'The-Slot',
					formatter: function (value, row, index) {
						return row.recordDate + ' ' + row.slot.fullTime
					}
				}, {
					field: 'recordPrice',
					title: 'Unit-Price',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'recordTime',
					title: 'Created',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'recordPay',
					title: 'Payment-Status',
					formatter: function (value, row, index) {
						var pstr = 'Paid </br>' + row.recordPayTime
						return ['Not', pstr][value]
					}
				}, {
					field: 'recordConfirm',
					title: 'Confirm-Status',
					formatter: function (value, row, index) {
						var pstr = 'Confirm </br>' + row.recordConfirmTime
						return ['Not', pstr][value]
					}
				}, {
					title: 'Operate',
					field: 'recordId',
					class: 'text-center',
					width: '20%',
					formatter: function (value, row, index) {
						let res = ''
						if(row.recordConfirm == 0){
							res += '<button class="btn btn-sm btn-warning" onclick="modifyRecordModal(\'' + value + '\')">Modify</button>&nbsp;';
						}
						if(row.recordPay == 1 && row.recordConfirm == 0){
							res += '<button class="btn btn-sm btn-primary " onclick="confirmRecord(\'' + value + '\')">Confirm</button>';
						}
						return res;
					}
				},],
				onPostBody: function (value) {
					$(".fixed-table-container").css('padding-bottom', '37px');
				}
			});
		}
		function closeRecordModal() {
			recordFormValidate.resetForm()
			$('#recordModal').modal('hide')
		}
		function modifyRecordModal(recordId) {
			var data = $("#recordTable").bootstrapTable('getRowByUniqueId', recordId);
			$('#recordId').val(data.recordId)
			$('#recordDate').val(data.recordDate)
			$('#recordPrice').val(data.recordPrice)
			$('#recordModal').modal('show')
		}
		function saveRecord(data) {
			$.post(ctx + "/admin/updateRecord", data, function (resp) {
				if (resp.status == 0) {
					loadRecordTable();
					$('#recordModal').modal('hide');
				}
				layer.msg(resp.msg);
			})
		}
		function confirmRecord(recordId) {
			layer.msg("Do you want to confirm it", {
				title: 'tip', icon: 3, btn: ["Confirm", "Cancel"],
				yes: function () {
					$.ajax({
						type: "get",
						url: ctx + "/admin/confirmRecord",
						data: {
							"recordId": recordId,
						},
						success: function (resp) {
							if (resp.status == 0) {
								loadRecordTable()
							}
							layer.msg(resp.msg);
						}
					});
				},
				btn2: function () {
					// layer.msg("Cancel");
				}
			});
		}
	</script>
</body>

</html>